//
//  ICEJavaScriptBridge.m
//  ICEH5Bridge
//
//  Created by wujianrong on 2017/8/14.
//  Copyright © 2017年 wanda. All rights reserved.
//

#import "ICEJavaScriptBridge.h"
#import "ICECore.h"
#import <WebKit/WebKit.h>
//#import <objc/objc-runtime.h>
#import "ICENewWebView.h"

@implementation ICEJavaScriptBridge

static NSString*  _static_prefix = nil;
+(void)configPluginWithPrefix:(NSString*)prefix
{
    _static_prefix = ICESafeString(prefix);
}

+ (NSString *) stringFromDictionary:(NSDictionary *) dictionary {
    if (![NSJSONSerialization isValidJSONObject:dictionary]) return @"";
    NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

+(void)sendReslutToJS:(NSDictionary*)result callInfo:(NSDictionary*)info
{
    [self sendReslutToJS:result callInfo:info completionHandler:nil];
}

+(void)sendReslutToJS:(NSDictionary*)result callInfo:(NSDictionary*)info completionHandler:(void (^)(id, NSError *error))completionHandler
{
    ICEWebInfo* vcinfo = [info objectForKey:@"vcinfo"];
    NSString* callbackId = [info objectForKey:@"id"];
    [self sendResultToJS:result callbackId:callbackId vcinfo:vcinfo completionHandler:completionHandler];
}

+(void)sendResultToJS:(NSDictionary*)result callbackId:(NSString*)callbackId vcinfo:(ICEWebInfo*)vcinfo completionHandler:(void (^)(id, NSError *error))completionHandler
{
    if ([result isKindOfClass:[NSDictionary class]] == NO) {
        return;
    }
    if (vcinfo.webView) {
        NSString *js = [NSString stringWithFormat:@"JSBridge.callback(\"%@\",%@);", ICESafeString(callbackId), [self stringFromDictionary:result]];
        WKWebView *webView = [(ICENewWebView*)vcinfo.webView webView];
        [webView evaluateJavaScript:js completionHandler:completionHandler];
    }
}

+(void)processJSWithPlugin:(NSString*)plugin method:(NSString*)method param:(NSDictionary*)param vcinfo:(ICEWebInfo*)vcinfo callbackId:(NSString*)callbackId
{
    NSString* icePlugin = nil;
    Class cls = NULL;
    if (ICESafeString(_static_prefix).length > 0) {
        //支持重写系统的plugin，用加前缀的方式
        icePlugin = [NSString stringWithFormat:@"%@%@", ICESafeString(_static_prefix), plugin];
        cls = NSClassFromString(icePlugin);
        if (cls == NULL) {
            icePlugin = [NSString stringWithFormat:@"ICE%@", plugin];
            cls = NSClassFromString(ICESafeString(icePlugin));
        }
    }
    else
    {
        icePlugin = [NSString stringWithFormat:@"ICE%@", plugin];
        cls = NSClassFromString(ICESafeString(icePlugin));
    }
    
    
    
    if (cls == NULL) {
        [self sendResultToJS:@{@"status":@"failed",@"message":@"不支持此plugin"} callbackId:(callbackId) vcinfo:vcinfo completionHandler:nil];
        return;
    }
    
    SEL sel = NSSelectorFromString([NSString stringWithFormat:@"%@:", ICESafeString(method)]);
    if (sel == NULL) {
        [self sendResultToJS:@{@"status":@"failed",@"message":@"不支持此方法"} callbackId:(callbackId) vcinfo:vcinfo completionHandler:nil];
        return;
    }
    
    id ins = [[cls alloc]init];
    if ([ins respondsToSelector:sel]) {
        NSMutableDictionary* dit = [[NSMutableDictionary alloc]init];
        if (param != nil) {
            [dit setObject:param forKey:@"param"];
        }
        [dit setObject:vcinfo forKey:@"vcinfo"];
        [dit setObject:ICESafeString(callbackId) forKey:@"id"];
        [ins performSelector:sel withObject:dit];
    }
    else
    {
        [self sendResultToJS:@{@"status":@"failed",@"message":@"不支持此方法"} callbackId:(callbackId) vcinfo:vcinfo completionHandler:nil];
        return;
    }
   
}
@end
